// 导入bcrypt加密中间件
const bcrypt = require('bcrypt')
const db = require('../db/index')
// 导入jwt，用于生成token
const jwt = require('jsonwebtoken')
// 导入jwt配置文件，用于加密跟解密
const jwtconfig = require('../jwt_config/index')

// 注册接口
exports.register = (req, res) => {
    // 获取前端传来的数据
    const reginfo = req.body
    // 1.判断前端传过来的账号是否存在数据表中
    const sql1 = 'select * from users where account = ?'
    db.query(sql1, reginfo.account, (err, results) => {
        if (results.length > 0) {
            return res.send({
                status: 1,
                message: '账号已存在'
            })
        }
        // 2.使用加密中间件bcrypt.js，对密码进行加密后的长度为10
        reginfo.password = bcrypt.hashSync(reginfo.password, 10)
        // 3.将账号和密码插入到数据表中
        const sql2 = 'insert into users set ?'
        db.query(sql2, {//sql语句/传入参数/函数
            account: reginfo.account,
            password: reginfo.password,
            identity: '用户',
            create_time: new Date(),
            status: 0
        }, (err, results) => {
            if (results.affectedRows !== 1) {
                return res.send({
                    status: 1,
                    message: '注册账号失败'
                })
            }
            return res.send({
                status: 0,
                message: '注册账号成功'
            })
        })
    })
}

//登录接口 account password =>
exports.login = (req, res) => {
    // 获取前端传来的参数
    const loginfo = req.body
    // 1.查看数据表中是否有传来的账号
    const sql1 = 'select * from users where account = ?'
    db.query(sql1, loginfo.account, (err, results) => {
        // 出错（一般数据库断开）返回错误信息
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('用户名不存在')
        // 2.对前端传来的密码进行解密比较
        const compareResult = bcrypt.compareSync(loginfo.password, results[0].password)
        if (!compareResult) {
            return res.cc('密码错误')
        }
        // 3.对账号是否冻结作判定
        if (results[0].status === 1) {
            return res.cc('账号被冻结')
        }
        // 4.生成返回给前端的token（剔除加密后的密码，头像，创建/更新时间）
        const user = {
            ...results[0],
            password: '',
            imageUrl: '',
            create_time: '',
            update_time: '',
        }
        // 设置token有效时长
        const tokenStr = jwt.sign(user, jwtconfig.jwtSecretKey, { expiresIn: '6h' })
        // 5.返回给前端的用户数据
        res.send({
            results: results[0],
            status: 0,
            message: '登录成功',
            token: 'Bearer ' + tokenStr,
        })
    })
}

// 后端写动态路由接口
// 最高管理员路由
const superAdminRouter = [
    {
        name: 'home',
        path: '/home',
        meta: { title: '首页' },
        component: 'home/index'
    },
    {
        name: 'setting',
        path: '/setting',
        meta: { title: '设置' },
        component: 'setting/index'
    },
    {
        name: 'overview',
        path: '/overview',
        meta: { title: '系统概览' },
        component: 'overview/index'
    },
    {
        name: 'product_manager',
        path: '/product_manager',
        meta: { title: '产品管理员' },
        component: 'user_manage/product_manager/index'
    },
    {
        name: 'message_manager',
        path: '/message_manager',
        meta: { title: '消息管理员' },
        component: 'user_manage/message_manager/index'
    },
    {
        name: 'user_list',
        path: '/user_list',
        meta: { title: '用户列表' },
        component: 'user_manage/user_list/index'
    },
    {
        name: 'user_manager',
        path: '/user_manager',
        meta: { title: '用户管理员' },
        component: 'user_manage/user_manager/index'
    },
    {
        name: 'product_list',
        path: '/product_list',
        meta: { title: '产品管理' },
        component: 'product_manage/product_list/index'
    },
    {
        name: 'out_list',
        path: '/out_list',
        meta: { title: '出库管理' },
        component: 'product_manage/out_list/index'
    },
    {
        name: 'message_list',
        path: '/message_list',
        meta: { title: '消息管理' },
        component: 'message_manage/message_list/index'
    },
    {
        name: 'recycle_list',
        path: '/recycle_list',
        meta: { title: '回收站' },
        component: 'message_manage/recycle_list/index'
    },
    {
        name: 'file',
        path: '/file',
        meta: { title: '合同管理' },
        component: 'file/index'
    },
    {
        name: 'operation_log',
        path: '/operation_log',
        meta: { title: '操作日志' },
        component: 'operation_log/index'
    },
    {
        name: 'login_log',
        path: '/login_log',
        meta: { title: '登录日志' },
        component: 'login_log/index'
    },
]
// 用户管理员员路由
const userAdminRouter = [
    {
        name: 'home',
        path: '/home',
        meta: { title: '首页' },
        component: 'home/index',
    },
    {
        name: 'setting',
        path: '/setting',
        meta: { title: '设置' },
        component: 'setting/index',
    },
    {
        name: 'overview',
        path: '/overview',
        meta: { title: '系统概览' },
        component: 'overview/index'
    },
    {
        name: 'user_list',
        path: '/user_list',
        meta: { title: '用户列表' },
        component: 'user_manage/user_list/index',
    },
    {
        name: 'file',
        path: '/file',
        meta: { title: '合同管理' },
        component: 'file/index',
    }
]
// 产品管理员路由
const productAdminRouter = [
    {
        name: 'home',
        path: '/home',
        meta: { title: '首页' },
        component: 'home/index',
    },
    {
        name: 'setting',
        path: '/setting',
        meta: { title: '设置' },
        component: 'setting/index',
    },
    {
        name: 'overview',
        path: '/overview',
        meta: { title: '系统概览' },
        component: 'overview/index'
    },
    {
        name: 'product_list',
        path: '/product_list',
        meta: { title: '产品管理' },
        component: 'product_manage/product_list/index',
    },
    {
        name: 'out_list',
        path: '/out_list',
        meta: { title: '出库管理' },
        component: 'product_manage/out_list/index',
    },
    {
        name: 'file',
        path: '/file',
        meta: { title: '合同管理' },
        component: 'file/index'
    },
]
// 消息管理员路由
const messageAdminRouter = [
    {
        name: 'home',
        path: '/home',
        meta: { title: '首页' },
        component: 'home/index',
    },
    {
        name: 'setting',
        path: '/setting',
        meta: { title: '设置' },
        component: 'setting/index',
    },
    {
        name: 'overview',
        path: '/overview',
        meta: { title: '系统概览' },
        component: 'overview/index'
    },
    {
        name: 'message_list',
        path: '/message_list',
        meta: { title: '消息管理' },
        component: 'message_manage/message_list/index'
    },
    {
        name: 'recycle_list',
        path: '/recycle_list',
        meta: { title: '回收站' },
        component: 'message_manage/recycle_list/index'
    },
    {
        name: 'file',
        path: '/file',
        meta: { title: '合同管理' },
        component: 'file/index'
    },
]
// 普通用户路由
const userRouter = [
    {
        name: 'home',
        path: '/home',
        meta: { title: '首页' },
        component: 'home/index'
    },
    {
        name: 'setting',
        path: '/setting',
        meta: { title: '设置' },
        component: 'setting/index'
    },
    {
        name: 'product_list',
        path: '/product_list',
        meta: { title: '产品管理' },
        component: 'product_manage/product_list/index'
    },
    {
        name: 'out_list',
        path: '/out_list',
        meta: { title: '出库管理' },
        component: 'product_manage/out_list/index'
    },
    {
        name: 'file',
        path: '/file',
        meta: { title: '合同管理' },
        component: 'file/index'
    },
]
// 返回用户对应的路由列表 用户id =>
exports.returnMenuList = (req, res) => {
    const sql = 'select identity from users where id = ?'
    db.query(sql, req.body.id, (err, result) => {
        if (err) return res.cc(err)
        let menu = []
        if (result[0].identity === '最高管理员') {
            menu = superAdminRouter
        }
        if (result[0].identity === '用户管理员') {
            menu = userAdminRouter
        }
        if (result[0].identity === '产品管理员') {
            menu = productAdminRouter
        }
        if (result[0].identity === '消息管理员') {
            menu = messageAdminRouter
        }
        if (result[0].identity === '用户') {
            menu = userRouter
        }
        res.send(menu)
    })
}